<template>
  <div class="account-edit">
    <el-dialog
      v-bind="$attrs"
      :title="state.dialogTitle"
      v-model="state.modalVisible"
      :before-close="cancelDialog"
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :lock-scroll="false"
      class="account-dialog"
      draggable
    >
      <el-form ref="accountRef" :model="state.form" :rules="state.rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名" prop="accountname">
              <el-input v-model="state.form.accountname" placeholder="用户名" :disabled="state.dialogTitle !== '新增'" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="state.dialogTitle === '新增'">
            <el-form-item label="密码" prop="accountpwd">
              <el-input v-model="state.form.accountpwd" placeholder="密码" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="支付名称" prop="accountnickpayname">
              <el-input v-model="state.form.accountnickpayname" placeholder="支付名称" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="昵称" prop="accountnickname">
              <el-input v-model="state.form.accountnickname" placeholder="昵称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色组" prop="accountgrouplist">
              <el-input v-model="state.form.accountgrouplist" placeholder="角色组" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="state.dialogTitle === '新增'">
            <el-form-item label="账户类型" prop="accounttype">
              <el-select v-model="state.form.accounttype" placeholder="账户类型">
                <el-option key="商户" label="商户" value="商户"></el-option>
                <el-option key="管理员" label="管理员" value="管理员"></el-option>
                <el-option key="代理商" label="代理商" value="代理商"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <!-- <el-col :span="12">
            <el-form-item label="上级id" prop="accountsuperaccountid">
              <el-input v-model="state.form.accountsuperaccountid" placeholder="上级id" />
            </el-form-item>
          </el-col> -->

          <!-- <el-col :span="12">
            <el-form-item label="账号创建人" prop="accountcreateuser">
              <el-input v-model="state.form.accountcreateuser" placeholder="账号创建人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号开通时间" prop="accountcreatedate">
              <el-input v-model="state.form.accountcreatedate" placeholder="账号开通时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号到期时间" prop="accountenddate">
              <el-input v-model="state.form.accountenddate" placeholder="账号到期时间" />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="电话号码" prop="accountlxrphone">
              <el-input v-model="state.form.accountlxrphone" placeholder="电话号码" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="账号是否可用" prop="accountstatus">
              <el-radio-group v-model="state.form.accountstatus">
                <el-radio :value="1" :label="1">可用</el-radio>
                <el-radio :value="0" :label="0">不可用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="tips-box" v-if="!Object.keys(state.editView).length">
        <p>
          <el-icon>
            <InfoFilled />
          </el-icon>
          <span>新建账号成功后，默认密码：123456</span>
        </p>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {aliccc_allin} from '@/api/base'

import useAccountManageStore from '@store/modules/accountManage'
import {filterObject} from '@/utils/index'
import md5 from 'js-md5'
import {nextTick} from 'vue'
import useAppStore from '@store/modules/app'
const accountManageStore = useAccountManageStore()
const {proxy} = getCurrentInstance()
const accountRef = ref(null)
import {setSession, getSession} from '@/utils/auth'
import {_view, _export, _add, _adds, _up, _ups, _del} from '@/api/app'

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  form: {
    accountname: '',
    accountnickpayname: '',
    accountssqd: '',
    accounttjsid: '',
    accounttjscode: '',
    accountgsdz: '',
    accountlsrsbh: '',
    accountqylx: '',
    accountsshy: '',
    accountlxr: '',
    accountlxrphone: '',
    accountainame: '',
    accountjflx: '',
    accounttxtnum: '',
    accounttxtimgnum: '',
    accountgrouplist: '',
    accountpopedom: '',
    accounttype: '',
    accountpwd: '',
    accountnickname: '',
    // accountcreateuser: '',
    // accountcreatedate: '',
    // accountenddate: '',
    accountywjs: '',
    accountstatus: '',
    accountsuperaccountid: '', // 上级目录
    accountalicccinstanceid: ''
  },
  organizationArr: [],
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
    accountstatus: [{required: true, message: '账号是否可用不能为空', trigger: 'blur'}]
  },
  example: []
})

const emits = defineEmits(['refresh'])

const getDataView = (accountid) => {
  nextTick(async () => {
    const result = await accountManageStore.view({'accountid.value': accountid})
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
    state.form.accountsuperaccountid = getSession('accountid') || ''
  })
}

const accountorChang = (data) => {
  state.form.accountorganizationid = data
}

const submitDialog = () => {
  accountRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.accountid = state.editView.accountid
        state.form['accountid.value'] = state.editView.accountid
        delete state.form.accountsuperaccountid
        delete state.form.accountname
        await accountManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        const newPram = JSON.parse(JSON.stringify(state.form))
        newPram.accountpwd = newPram.accountpwd ? md5(newPram.accountpwd) : md5('123456')
        await accountManageStore.add(newPram)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      accountRef.value.resetFields()
      state.modalVisible = false
      console.log(state.modalVisible, 'state.modalVisible')
      emits('refresh')
    }
  })
}

/** 实例列表 */
const getExample = async () => {
  const response = await aliccc_allin({PageNumber: 1, PageSize: 99, pagevalue: 221})
  state.example = response?.Data?.List
}

const cancelDialog = () => {
  state.editView = {}
  accountRef.value.resetFields()
  state.modalVisible = false
}

const openDialog = async (data = {}) => {
  state.organizationArr = useAppStore().organizationtreeData
  if (Object.keys(data).length) {
    await getDataView(data.accountid)
  }
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.accountname}】` : '新增'
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
